package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Client;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.ClientService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/client")
public class ClientController {
    @Autowired
    private ClientService clientService;
    @RequiredPermission("客户查询")
    @RequestMapping("/list")
    public String list(Map map, QueryObject qo) {
        PageResult result = clientService.query(qo);
        System.out.println(qo.getPageSize());
        System.out.println("=========================================");
        map.put("result",result);
        return "client/list";
    }
    @RequiredPermission("客户编辑")
    @RequestMapping("/input")
    public String input(Map map, Long id) {
        if (id != null && id != 0) {
            Client client = clientService.getById(id);
            map.put("client", client);
        }
        return "/client/input";
    }
    @RequiredPermission("客户删除")
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        clientService.deleteById(id);
        try {

        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return  JSONResult.success("删除成功");
    }
    @RequiredPermission("客户新增/修改")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Client dapt) {
        try {

            clientService.updateOrSave(dapt);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return JSONResult.success("新增/修改成功");
    }
@RequestMapping("deleteall")
@ResponseBody
public JSONResult deleteall(Long[] ids) {
    try {
        if (ids==null){
            return JSONResult.mark("没有选中");
        }
        clientService.deleteall(ids);
    }catch (Exception e){
        e.printStackTrace();
        return JSONResult.mark(e.getMessage());
    }
    return JSONResult.success("删除成功");
}

}
